package com.chat.login.controller;

import com.chat.common.dto.AuthRequestDTO;
import com.chat.common.vo.R;
import com.chat.login.annotate.IsNumeric;
import com.chat.login.domain.dto.CheckMailDTO;
import com.chat.login.domain.vo.LoginUserVO;
import com.chat.login.service.AuthService;
import jakarta.validation.constraints.NotBlank;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户校验
 */
@RestController
@Slf4j
@RequestMapping("/auth/auth")
public class AuthController {

    @Autowired
    AuthService authService;

    /**
     * 验证token
     * @param authRequestDTO
     * @return
     */
    @PostMapping("/validate_token")
    public R validateToken(@RequestBody AuthRequestDTO authRequestDTO ) {
        log.info("URL: " + authRequestDTO.getUrl());
        log.info("Token: " + authRequestDTO.getToken());
        return authService.validateToken(authRequestDTO);
    }

    /**
     * 刷新token
     * @param token
     * @return
     */
    @GetMapping("/refresh_token")
    public R<LoginUserVO> refreshToken(@RequestParam("token") String token) {
        return authService.refreshToken(token);
    }

    /**
     * 根据账号获取邮箱
     * @param account
     * @return
     */
    @GetMapping("/getMail")
    public R getAccountOfMail(@RequestParam @NotBlank(message = "账号不能为空") @IsNumeric(message = "账号不是数字") String account){
        return authService.getAccountOfMail(account);
    }

    /**
     * 判断邮箱是否是正确的
     * @param checkMailDTO
     * @return
     */
    @PostMapping("/checkMail")
    public R checkMailAndAccount(@RequestBody @Validated CheckMailDTO checkMailDTO){
        return authService.checkMailAndAccount(checkMailDTO);
    }

    /**
     * 判断账号状态，并发送验证码
     * @param account
     * @return
     */
    @GetMapping("/getMail_state")
    public R BooleanAccountState(@RequestParam @NotBlank(message = "账号不能为空") @IsNumeric(message = "账号不是数字") String account){
        return authService.BooleanAccountState(account);
    }
}
